package me.sealer.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.constant.ErrorCodeConstant;
import me.sealer.ssm.constant.PageConstant;
import me.sealer.ssm.exception.SsmException;
import me.sealer.ssm.mapper.PermissionMapper;
import me.sealer.ssm.mapper.RolePermissionMapper;
import me.sealer.ssm.model.Permission;
import me.sealer.ssm.model.PermissionExample;
import me.sealer.ssm.model.RolePermissionExample;
import me.sealer.ssm.page.PageResult;
import me.sealer.ssm.service.PermissionService;
import me.sealer.ssm.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sealer
 * @date 2018/05/11.
 */
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {
    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper rolePermissionMapper;

    public PermissionServiceImpl(PermissionMapper permissionMapper, RolePermissionMapper rolePermissionMapper) {
        this.permissionMapper = permissionMapper;
        this.rolePermissionMapper = rolePermissionMapper;
    }

    @Override
    public List<Permission> queryAllPermissions() {
        return permissionMapper.selectByExample(null);
    }

    @Override
    public PageResult<Permission> queryPermissionsPage(int pageNo, int pageSize) {
        pageSize = Math.min(pageSize, PageConstant.MAX_PAGE_SIZE);

        PermissionExample example = new PermissionExample();
        int count = permissionMapper.countByExample(example);

        PageHelper.startPage(pageNo, pageSize);
        List<Permission> permissions = permissionMapper.selectByExample(example);

        return PageUtil.generatePageResult(pageNo, pageSize, permissions, count);
    }

    @Override
    public void newPermission(Permission permission) {
        // 主键id自增， 避免人为控制
        permission.setId(null);
        permissionMapper.insertSelective(permission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePermission(Integer id, String permission) {
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.createCriteria()
                .andIdEqualTo(id)
                .andPermissionEqualTo(permission);
        int permissionCount = permissionMapper.countByExample(permissionExample);

        // 权限不存在则无法删除， 抛错
        if (permissionCount == 0) {
            throw new SsmException(ErrorCodeConstant.PERMISSION_NOT_EXISTS);
        }

        // 检查权限是否被角色占用， 若占用，不允许删除权限
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andPermissionIdEqualTo(id);
        int rolePermissionCount = rolePermissionMapper.countByExample(rolePermissionExample);
        if (rolePermissionCount > 0) {
            throw new SsmException(ErrorCodeConstant.PERMISSION_ALREADY_USED);
        }

        permissionMapper.deleteByPrimaryKey(id);
    }
}
